using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using ImPM.Data.ManagerObjects;
using ImPM.Data.BusinessObjects;
using ImPM.Data.Base;

namespace ImPM.Data.UnitTests
{
	[TestFixture]
    public partial class ResourceTests : UNuitTestBase
    {
        protected IResourceManager manager;
		
		public ResourceTests()
        {
            manager = managerFactory.GetResourceManager();
        }
		
		protected Resource CreateNewResource()
		{
			Resource entity = new Resource();
			
			// You may need to maually enter this key if there is a constraint violation.
			entity.Id = 62;
			
			entity.Type = default(SByte);
			entity.ResourceName = "Test Test ";
			entity.Overallocated = default(UInt16);
			entity.StandardRate = 96;
			entity.CalendarID = 28;
			entity.CurrencyID = 48;
			entity.OvertimeAllowed = default(UInt16);
			entity.OvertimeFactor = 56;
			entity.Email = "Test Test Test Test Test ";
			entity.Phone = "Test Test Test Tes";
			entity.ExtensionLine = "Test Test Test ";
			entity.Note = "Test Test ";
			entity.Active = default(UInt16);
			entity.UserID = 15;
			entity.UnitID = 50;
			
			return entity;
		}
		protected Resource GetFirstResource()
        {
            IList<Resource> entityList = manager.GetAll(1);
            if (entityList.Count == 0)
                Assert.Fail("All tables must have at least one row for unit tests to succeed.");
            return entityList[0];
        }
		
		[Test]
        public void Create()
        {
            try
            {
				Resource entity = CreateNewResource();
				
                object result = manager.Save(entity);

                Assert.IsNotNull(result);
            }
            catch(Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        [Test]
        public void Read()
        {
            try
            {
                Resource entityA = CreateNewResource();
				manager.Save(entityA);

                Resource entityB = manager.GetById(entityA.Id);

                Assert.AreEqual(entityA, entityB);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
		[Test]
		public void Update()
        {
            try
            {
                Resource entityA = GetFirstResource();
				entityA.Type = default(SByte);
				
				manager.Update(entityA);

                Resource entityB = manager.GetById(entityA.Id);

                Assert.AreEqual(entityA.Type, entityB.Type);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        [Test]
        public void Delete()
        {
            try
            {
                Resource entity = GetFirstResource();
				
                manager.Delete(entity);

                entity = manager.GetById(entity.Id);
                Assert.IsNull(entity);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
	}
}